package com.nageoffer.shortlink.admin.controller;

import com.nageoffer.shortlink.admin.common.convention.result.Result;
import com.nageoffer.shortlink.admin.common.convention.result.Results;
import com.nageoffer.shortlink.admin.common.enums.UserErrorCodeEnum;
import com.nageoffer.shortlink.admin.dto.req.UserLoginReqDto;
import com.nageoffer.shortlink.admin.dto.req.UserRegisterReqDto;
import com.nageoffer.shortlink.admin.dto.req.UserUpdateReqDto;
import com.nageoffer.shortlink.admin.dto.resp.UserLoginRespDto;
import com.nageoffer.shortlink.admin.dto.resp.UserRespDto;
import com.nageoffer.shortlink.admin.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    /**
     * 根据用户名查询用户
     * @param username
     * @return
     */
    @GetMapping("/api/short-link/admin/v1/user/{username}")
    public Result<UserRespDto> getUserByUsername(@PathVariable String username) {
        UserRespDto result = userService.getUserByUsername(username);
        return Results.success(result);
    }

    /**
     * 查询用户是否存在
     * @param username
     * @return
     */
    @GetMapping("/api/short-link/admin/v1/user/hasUsername")
    public Result<Boolean> hasUsername(@RequestParam("username") String username) {
        return Results.success(userService.hasUsername(username));
    }

    @PostMapping("/api/short-link/admin/v1/user/register")
    public Result<Void> register(@RequestBody UserRegisterReqDto requestParam) {
//        System.out.println(com.google.common.base.Preconditions.class.getName());
        userService.register(requestParam);
        return Results.success();
    }
    @PutMapping("/api/short-link/admin/v1/user")
    public Result<Void> modify(@RequestBody UserUpdateReqDto requestParam) {
        userService.update(requestParam);
        return Results.success();
    }

    @GetMapping("/api/short-link/admin/v1/user/login")
    public Result<UserLoginRespDto> login(@RequestBody UserLoginReqDto requestParam) {
        UserLoginRespDto userLoginRespDto = userService.login(requestParam);
        return Results.success(userLoginRespDto);
    }

    @GetMapping("/api/short-link/admin/v1/user/check-login")
    public Result<Boolean> checkLogin(@RequestParam("username")String username,@RequestParam("token")String token) {
        Boolean flag = userService.checkLogin(username,token);
        return Results.success(flag);
    }

    @DeleteMapping("/api/short-link/admin/v1/user/logout")
    public Result<Void> logout(@RequestParam("username")String username,@RequestParam("token")String token) {
        userService.logout(username,token);
        return Results.success();
    }

    @PostMapping("/api/short-link/admin/v1/user/add-bloom")
    public Result<Void> addBloom(@RequestParam("username")String username) {
        userService.addBloom(username);
        return Results.success();
    }
}
